import random
from collections import Counter

import pandas as pd


def generate_wind_speeds(n):
    # 添加7级风范围
    wind_level_ranges = {
        1: (0.3, 1.5),
        2: (1.6, 3.3),
        3: (3.4, 5.4),
        4: (5.5, 7.9),
        5: (8.0, 10.7),
        6: (10.8, 13.8),
        7: (13.9, 17.1)  # 添加7级风范围
    }

    # 生成常规风速（n-5个）
    normal_speeds = []
    for _ in range(n - 5):
        rand = random.random()
        if rand < 0.9547:  # 调整后的概率分界点（原95%占比）
            level = random.choices([1, 2, 3], weights=[40, 40, 15], k=1)[0]
        else:  # 原4.5%占比
            level = random.choice([4, 5])

        lower, upper = wind_level_ranges[level]
        normal_speeds.append(round(random.uniform(lower, upper), 1))

    # 生成强制5个6/7级风
    extra_speeds = []
    for _ in range(5):
        level = random.choice([6, 7])  # 随机选择6或7级
        lower, upper = wind_level_ranges[level]
        extra_speeds.append(round(random.uniform(lower, upper), 1))

    # 合并并打乱顺序
    all_speeds = normal_speeds + extra_speeds
    random.shuffle(all_speeds)

    return all_speeds


# 生成数据并写入Excel
n = 1500
wind_speeds = generate_wind_speeds(n)

# 创建DataFrame并保存
df = pd.DataFrame(wind_speeds, columns=['Wind Speed (m/s)'])
df.to_excel('wind_speeds.xlsx', index=False)

# 验证分布
levels = []
for s in wind_speeds:
    if s <= 1.5:
        levels.append(1)
    elif s <= 3.3:
        levels.append(2)
    elif s <= 5.4:
        levels.append(3)
    elif s <= 7.9:
        levels.append(4)
    elif s <= 10.7:
        levels.append(5)
    elif s <= 13.8:
        levels.append(6)
    else:
        levels.append(7)

count = Counter(levels)
print("各等级分布:", dict(sorted(count.items())))
print("6-7级出现次数:", count[6] + count[7])  # 应该接近5次